<?php
namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Models\StorageModel;
use App\V3\V3_2\Models\StorageShelfLocationModel;
use App\V3\V3_2\Models\StorageShelfModel;
use App\V3\V3_2\Services\ShelfService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 库位管理
 * AUTHOR  : zl
 * DateTime: 2022/08/22
 */
class LocationController extends BaseController
{

    /**
    添加库位
     */
    public function LocationCreate(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'locationName' =>'required|string',//库位名称
            'shelfId' =>'required|integer',//货架ID
            'storageId' =>'required|integer',//仓库ID
        ],[
            'locationName.required' => '参数`locationName`缺失',
            'locationName.string' => '参数`locationName`仅能为字符串',
            'shelfId.required' => '参数`shelfId`缺失',
            'shelfId.integer' => '参数`shelfId`仅能为数字',
            'storageId.required' => '参数`storageId`缺失',
            'storageId.integer' => '参数`storageId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if(empty(ShelfService::is_use_shelf($this->getMainUserId()))){
            return $this->fail('','未开启货架功能');
        }

        $location = new StorageShelfLocationModel();
        $location->location_name = $request->locationName;
        $location->main_id = $this->getMainUserId();
        $location->storage_id = $request->storageId;
        $location->shelf_id = $request->shelfId;
        $location->location_type = StorageShelfLocationModel::CREATETYPE;
        $location->remark = empty($data['remark']) ? '' : $data['remark'];
        $location->sort = empty($data['sort']) ? 0 : $data['sort'];

        $res = $location->save();

        if($res){
            return $this->success('','添加成功');
        }else{
            return $this->fail('','添加失败');
        }
    }

    /**
    添加库位
     */
    public function LocationUpdate(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'locationName' =>'required|string',//库位名称
//            'shelfId' =>'required|integer',//货架ID
            'locationId' =>'required|integer',//库位ID
        ],[
            'locationName.required' => '参数`locationName`缺失',
            'locationName.string' => '参数`locationName`仅能为字符串',
//            'shelfId.required' => '参数`shelfId`缺失',
//            'shelfId.integer' => '参数`shelfId`仅能为数字',
            'locationId.required' => '参数`locationId`缺失',
            'locationId.integer' => '参数`locationId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if(empty(ShelfService::is_use_shelf($this->getMainUserId()))){
            return $this->fail('','未开启货架功能');
        }

        $info = StorageShelfLocationModel::where('id', $data['locationId'])
            ->where('main_id',$this->getMainUserId())
            ->where('status',StorageShelfModel::USEDSTATUS)->first();
        if($info['location_type'] == StorageShelfModel::DEFAULTTYPE){
            return $this->fail('','默认库位不允许修改');
        }

        $res = StorageShelfLocationModel::where('id', $data['locationId'])
            ->where('main_id',$this->getMainUserId())
            ->where('status',StorageShelfModel::USEDSTATUS)
            ->update(['location_name' => $data['locationName'],
                'remark' => empty($data['remark'])?"":$data['remark'],
                'sort' => empty($data['sort'])?1:$data['sort']]);

       if($res){
            return $this->success('','修改成功');
        }else{
            return $this->fail('','修改失败');
        }

    }

    public function LocationDel(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'locationId' =>'required|integer',//货架ID
        ],[
            'locationId.required' => '参数`locationId`缺失',
            'locationId.integer' => '参数`locationId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if(empty(ShelfService::is_use_shelf($this->getMainUserId()))){
            return $this->fail('','未开启货架功能');
        }

        $location_info = StorageShelfLocationModel::where('id',$data['locationId'])->first();

        if($location_info['location_type'] == StorageShelfLocationModel::DEFAULTTYPE){
            return $this->fail('','默认库位不能删除');
        }

        $judging_status = ShelfService::judging_del($data['locationId'],3,$this->getMainUserId());

        if(!empty($judging_status)){
            $res = StorageShelfLocationModel::where('id', $data['locationId'])
                ->where('main_id',$this->getMainUserId())
                ->where('status',StorageShelfLocationModel::USEDSTATUS)
                ->update(['status' => StorageShelfLocationModel::DELETESTATUS]);

            if($res){
                return $this->success('','删除成功');
            }else{
                return $this->fail('','删除失败');
            }
        }else{
            return $this->fail('','此库位还有库存商品，不能删除');
        }

    }

    public function LocationList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'shelfId' =>'required|integer',//货架ID
        ],[
            'shelfId.required' => '参数`storageId`缺失',
            'shelfId.integer' => '参数`storageId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        return $this->success(
            ShelfService::location_list($this->getMainUserId(),$data)
        );
    }

    /**
    库位详情
     */
    public function LocationInfo(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'locationId' =>'required|integer',//货架ID
        ],[
            'locationId.required' => '参数`locationId`缺失',
            'locationId.string' => '参数`locationId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $info = StorageShelfLocationModel::where('id', $data['locationId'])
            ->where('main_id',$this->getMainUserId())
            ->where('status',StorageShelfModel::USEDSTATUS)->first();

        if(empty($info)){
            return $this->fail('','没查询到相关数据');
        }

        $storage = StorageModel::where(['id' => $info['storage_id']])->first();
        $shelf = StorageShelfModel::where(['id' => $info['shelf_id']])->first();

        $res = [];
        $res['storage_name'] = $storage['storage_name'];
        $res['storage_id'] = $storage['id'];
        $res['shelf_id'] = $shelf['id'];
        $res['shelf_name'] = $shelf['shelf_name'];
        $res['location_id'] = $info['id'];
        $res['location_name'] = $info['location_name'];
        $res['remark'] = $info['remark'];
        $res['sort'] = $info['sort'];

        return $this->success($res,'获取成功');


    }
}
